import Print from "./print";
import { compare, deleteZeroArr } from "./utils";
/**
 * @func 两数组字符串相加
 * @param {*} param1
 * @param {*} param2
 */
export const resolveAdd = (arr1, arr2) => {
  if (!arr1 || !arr1.length) {
    Print.error("resolveAdd第一个参数不可为空");
    return [];
  }

  if (!arr2 || !arr2.length) {
    return arr1;
  }
  const result: number[] = [];
  const len = arr1.length > arr2.length ? arr1.length : arr2.length;
  let remainder = 0; // 用于10以内数相加是否大于10
  for (let index = 0; index < len || remainder > 0; index++) {
    result[index] =
      (remainder += Number(arr1[index] || 0) + Number(arr2[index] || 0)) % 10;
    remainder = Math.floor(remainder / 10);
  }
  return result;
};

/**
 * @func 两数组字符串相减, 第一个数组一定大于第二个
 * @param {Array} arr1
 * @param {Array} arr1
 */
export const resolveSubtract = (arr1, arr2) => {
  if (!arr1 || !arr1.length) {
    Print.error("resolveSubtract第一个参数不可为空");
    return [];
  }

  if (!arr2 || !arr2.length) {
    return arr1;
  }

  const result: number[] = [];
  const len = arr1.length;
  let remainder = 0; // 用于10以内数相减借位使用
  for (let index = 0; index < len; index++) {
    result[index] =
      arr1[index] - (Number(arr2[index] || 0) + Number(remainder));
    // 出现负数，需要借位
    result[index] += (remainder = result[index] >= 0 ? 0 : 1) * 10;
  }
  return result;
};

/**
 * @func 两字符串数组相乘
 * @param {Array} arr1
 * @param {Array} arr2
 */
export const resolveMultiply = (arr1, arr2) => {
  if (!arr1 || !arr1.length) {
    Print.error("resolveMultiply第一个参数不可为空");
    return [0];
  }
  if (!arr2 || !arr2.length) {
    return arr1;
  }
  const result: number[] = [];
  let remainder = 0; // 用于10以内数相加是否大于10
  for (let arr1Index = 0; arr1Index < arr1.length; arr1Index++) {
    for (
      let arr2Index = 0;
      arr2Index < arr2.length || remainder > 0;
      arr2Index++
    ) {
      result[arr1Index + arr2Index] =
        (remainder +=
          (result[arr1Index + arr2Index] || 0) +
          Number(arr1[arr1Index] || 0) * Number(arr2[arr2Index] || 0)) % 10;
      remainder = Math.floor(remainder / 10);
    }
  }
  return result;
};

/**
 * @return {result, decimal}
 * @func 两字符串数组相除（除法较为特殊需要，进行余数处理，即处理小数位数，默认最大30位小数）
 * @param {Array} arr1
 * @param {Array} arr2
 */
export const resolveDivision = (
  arr1: string[],
  arr2: string[],
  decimalLength
) => {
  if (!arr1 || !arr1.length) {
    Print.error("resolveDivision");
    return {
      result: [0],
      decimalLength,
    };
  }
  if (!arr2 || !arr2.length) {
    return {
      result: arr1,
      decimalLength,
    };
  }
  let defaultZeroSup = 30;
  if (arr2.length > arr1.length) {
    defaultZeroSup += arr2.length - arr1.length;
  }
  let addArr: string[] = [];
  let newDecimalLength = decimalLength;
  // 如果被除数位数大于除数位数30位无需补零
  if (arr1.length - arr2.length < 30) {
    addArr = new Array(defaultZeroSup).fill("0");
    newDecimalLength = decimalLength + defaultZeroSup;
  }
  // 去除 被除数尾部多于0
  const divisorArr = deleteZeroArr(addArr.concat(arr1));
  // 去除 除数尾部多于0
  const devisionArr = deleteZeroArr(arr2);
  const result: number[] = [];
  let rest = [0];
  for (let index = divisorArr.length - 1; index >= 0; index--) {
    rest = resolveMultiply(rest, [0, 1]);
    rest[0] = Number(divisorArr[index]);
    /**去0**/
    rest = deleteZeroArr(rest).map((item) => Number(item));
    result[index] = 0;
    while (compare(devisionArr, rest) <= 0) {
      result[index]++;
      rest = resolveSubtract(rest, devisionArr);
      /**去0**/
      rest = deleteZeroArr(rest).map((item) => Number(item));
    }
  }
  return { result, decimalLength: newDecimalLength };
};
